import {
	BigNumber
} from 'bignumber.js';

/**
 * 加法
 * @param number1 [1,2,3,4] || 1 当为Array时则直接计算数组的和
 * @param number2 any
 * @returns number
 */
export const addCalc = (number1, number2) => {
	if (!number1 && number1 !== 0) return 0
	if (Array.isArray(number1)) {
		return Number(BigNumber.sum.apply(null, number1))
	} else {
		return Number(BigNumber.sum(number1, number2))
	}
}

/**
 * 减法
 * @param number1
 * @param number2
 * @returns number
 */
export const subtractCalc = function(number1, number2) {
	if (!number1 && number1 !== 0) return 0
	return Number(new BigNumber(number1).minus(number2))
}

/**
 * 乘以
 * @param number1
 * @param number2
 * @returns number
 */
export const multiplyCalc = function(number1, number2) {
	if (!number1 && number1 !== 0) return 0
	if (!number2 && number2 !== 0) return 0
	return Number(new BigNumber(number1).times(number2))
}

/**
 * 除以
 * @param number1
 * @param number2
 * @returns number
 */
export const divideClalc = function(number1, number2) {
	if (!number1 && number1 !== 0) return 0
	if (number2 == 0) return 0
	return Number(new BigNumber(number1).div(number2))
}

export const getObjectByKey = (array, key, value) => {
	if (!array || array.length == 0) return null
	return array.find(item => item[key] == value)
}